<?php
/**
 * Created by PhpStorm.
 * User: zhoper
 * Date: 2018/7/28
 * Time: 15:12
 */
namespace app\widgets;

class Curl{
    /**
     * @desc 封装curl的调用接口，post的请求方式
     */
    public static function POST($url, $requestString, $timeout = 5) {
        if($url == "" || $requestString == "" || $timeout <= 0){
            return false;
        }
        $con = curl_init((string)$url);
        curl_setopt($con, CURLOPT_HEADER, false);
        curl_setopt($con, CURLOPT_POSTFIELDS, $requestString);
        curl_setopt($con, CURLOPT_POST, true);
        curl_setopt($con, CURLOPT_RETURNTRANSFER,true);
        curl_setopt($con, CURLOPT_TIMEOUT, (int)$timeout);
        curl_setopt($con, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($con, CURLOPT_SSL_VERIFYHOST, false);

        return curl_exec($con);
    }

    /**
     * @desc 封装curl的调用接口，post的请求方式
     */
    public static function POST_HTTPS($url, $requestString,$pemfile, $timeout = 5) {
        if($url == "" || $requestString == "" || $timeout <= 0){
            return false;
        }

        $con = curl_init((string)$url);
        curl_setopt($con, CURLOPT_HEADER, false);
        curl_setopt($con, CURLOPT_POSTFIELDS, $requestString);
        curl_setopt($con, CURLOPT_POST, true);
        curl_setopt($con, CURLOPT_RETURNTRANSFER,true);
        curl_setopt($con, CURLOPT_TIMEOUT, (int)$timeout);
        curl_setopt($con, CURLOPT_SSL_VERIFYPEER, 1);
        curl_setopt($con, CURLOPT_SSL_VERIFYHOST, 2);
        //curl_setopt($ch, CURLOPT_SSLCERT, $pemfile);
        //curl_setopt($ch, CURLOPT_SSLCERTTYPE, 'PEM');
        curl_setopt($con, CURLOPT_CAINFO, $pemfile);

        return curl_exec($con);
    }

    /**
     * @desc 封装curl的调用接口，post Json数据的请求方式
     */
    public static function POST_JSON($url, $data, $timeout = 25){
        if($url == "" || empty($data) || $timeout <= 0){
            return false;
        }
        $requestString = json_encode($data);
        $con = curl_init((string)$url);
        curl_setopt($con, CURLOPT_HEADER, false);
        curl_setopt($con, CURLOPT_POSTFIELDS, $requestString);
        curl_setopt($con, CURLOPT_POST, true);
        curl_setopt($con, CURLOPT_RETURNTRANSFER,true);
        curl_setopt($con, CURLOPT_TIMEOUT, (int)$timeout);
        curl_setopt($con, CURLOPT_HTTPHEADER, array(
                'Content-Type: application/json',
                'Content-Length: ' . strlen($requestString))
        );
        return curl_exec($con);
    }

    /**
     * @desc 封装curl的调用接口，post form数据的请求方式
     */
    public static function POST_FORM($url, $requestString, $timeout = 25){
        if($url == "" || $requestString == "" || $timeout <= 0){
            return false;
        }
        $con = curl_init((string)$url);
        curl_setopt($con, CURLOPT_HEADER, false);
        curl_setopt($con, CURLOPT_POSTFIELDS, $requestString);
        curl_setopt($con, CURLOPT_POST, true);
        curl_setopt($con, CURLOPT_RETURNTRANSFER,true);
        curl_setopt($con, CURLOPT_TIMEOUT, (int)$timeout);
        curl_setopt($con, CURLOPT_HTTPHEADER, array(
                'Content-Type: application/x-www-form-urlencoded')
        );
        return curl_exec($con);
    }

    /**
     * @desc 封装curl的调用接口，get的请求方式
     */
    public static function GET($url, $data = array(), $timeout = 10) {
        if($url == "" || $timeout <= 0){
            return false;
        }
        if($data != array()) {
            $url = $url . '?' . http_build_query($data);
        }
        $con = curl_init((string)$url);
        curl_setopt($con, CURLOPT_HEADER, false);
        curl_setopt($con, CURLOPT_RETURNTRANSFER,true);
        curl_setopt($con, CURLOPT_TIMEOUT, (int)$timeout);
        curl_setopt($con, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($con, CURLOPT_SSL_VERIFYHOST, false);
        return json_decode(curl_exec($con),true);
    }
}